package com.itheima.service.impl;

import com.itheima.anno.Log;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceimpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept>  queryDeptList() {
        return deptMapper.findAll();
    }
    @Log
    @Override
    public void delete(Integer id) {
        Integer deptId=id;
        Long count = empMapper .countById(deptId);
        if(count>0){
            throw new BusinessException("该部门下有员工，不能删除");
        }
        deptMapper.delete(id);
    }
    @Log
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept findById(Integer id) {
        return deptMapper.findById(id);
    }
    @Log
    @Override
    public void update(Dept dept) {
        deptMapper.update(dept);
    }
}
